$(function () {
    var t = $('#receipt_table').DataTable(
            {
                "oLanguage": {
                    "sProcessing": "Đang xử lý...",
                    "sLengthMenu": "Xem _MENU_ mục",
                    "sZeroRecords": "Không tìm thấy dòng nào phù hợp",
                    "sInfo": "Đang xem _START_ đến _END_ trong tổng số _TOTAL_ mục",
                    "sInfoEmpty": "Đang xem 0 đến 0 trong tổng số 0 mục",
                    "sInfoFiltered": "(được lọc từ _MAX_ mục)",
                    "sInfoPostFix": "",
                    "sSearch": "Tìm:",
                    "sUrl": "",
                    "oPaginate": {
                        "sFirst": "Đầu",
                        "sPrevious": "Trước",
                        "sNext": "Tiếp",
                        "sLast": "Cuối"
                    }
                }
            }

    );
    var counter = 1;


    var i = 0;
    $("#search_product_txt").autocomplete({
        minLength: 0,
        source: projects,
        focus: function (event, ui) {
            $("#search_product_txt").val(ui.item.label);
            return false;
        },
        select: function (event, ui) {
            i++;
            //$("#search_product_txt").val(ui.item.label);
            //$("#project-id").val(ui.item.id);
            //$("#project-description").html(ui.item.sale_price);
            t.row.add([
                i,
                ui.item.label,
                '<input type="text" readonly="readonly" id="sale_price_' + i + '" value="' + ui.item.sale_price + '"/>',
                '<span style="cursor:pointer" onclick="minus(' + i + ')" class="glyphicon glyphicon-minus-sign"></span>'
                        + '<input type="text" style="width:35px" id="amount_txt_' + i + '" onkeyup="price_x_amount(' + i + ')" value="1" pattern="[0-9]"/>'
                        + '<span style="cursor:pointer" onclick="plus(' + i + ')" class="glyphicon glyphicon-plus-sign"></span>',
                '<input type="text" readonly="readonly" id="price_x_amount_txt_' + i + '" value="' + ui.item.sale_price + '"/>'
            ]).draw();


            return false;
        }
    })
            .autocomplete("instance")._renderItem = function (ul, item) {
        return $("<li>")
                .append("<a>" + item.label + "<br>" + item.sale_price + "</a>")
                .appendTo(ul);
    };


});
function price_x_amount(i) {

    var price = $('#sale_price_' + 1).val();
    var am = $('#amount_txt_' + i).val();
    var cal = price * am;

    $('#price_x_amount_txt_' + i).val(cal);
}

function plus(i) {
    var am = $('#amount_txt_' + i).val();
    am = parseInt(am);
    var new_am = am + 1;

    $('#amount_txt_' + i).val(new_am)
    var price = $('#sale_price_' + 1).val();
    var cal = price * new_am;

    $('#price_x_amount_txt_' + i).val(cal);

}

function minus(i) {
    var am = $('#amount_txt_' + i).val();
    am = parseInt(am);
    if (am>1){
        var new_am = am - 1;

        $('#amount_txt_' + i).val(new_am)
        var price = $('#sale_price_' + 1).val();
        var cal = price * new_am;

        $('#price_x_amount_txt_' + i).val(cal);

    }

}